<?php

namespace app\common\model;

use app\admin\controller\mall\GoodsSku;
use app\common\lib\Time;
use think\Exception;

class MallOrder extends Base
{
    protected $deleteTime = 'delete_time';

    /**
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getWechatAttr($value): array
    {
        $wechat = (new WeixinUser())->getWeixinUserByOpenid($value);
        if (!$wechat) {
            return [];
        } else {
            return $wechat->toArray();
        }
    }

    /**
     * 根据订单号查询数据
     * @param string $order_id
     * @param string $field
     * @param array|string $with
     * @return array
     * @throws Exception
     */
    public function getOrderByOrderId(string $order_id = "", string $field = "*",$with = null): array
    {
        //获取订单信息
        try {
            $result = $this->field($field)
                ->where('ordersn', $order_id)
                ->with($with)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("订单错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * @param string $openid
     * @return mixed
     */
    public function getUserOrderByCount(string $openid = '')
    {
        return $this->field('count(*) as count,status')
            ->where('openid', $openid)
            ->where('deleted',0)
            ->group('status')
            ->select()
            ->toArray();
    }

    /**
     * @param array $where
     * @param string $openid
     * @param int $limit
     * @return array
     * @throws Exception
     */
    public function getOrderByOpenidList(array $where = [], string $openid = '', int $limit = 10)
    {
        try {
            $row = $this
                ->where('openid', $openid)
                ->where($where)
                ->order('id desc')
                ->paginate($limit)
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("订单查询失败");
        }
        return $row;
    }

    /**
     * @return string[]
     */
    public function getStatusList()
    {
        return [
            '0' => '待支付',
            '1' => '已支付',
            '2' => '待收货',
            '3' => '已完',
            '4' => '售后',
            '5' => '支付中',
            '6' => '待取件',
            '7' => '备货中',
            '8' => '待接单',
            '9' => '取消订单'
        ];
    }

    /**
     * @return array
     * @throws Exception
     */
    public function overdueOrder(): array
    {
        $second = sysconfig('shop', 'order_time');
        $second = $second ?? 15;
        $time = time() - $second * 60;
        try {
            $arr = $this->where('create_time', '<', $time)
                ->where('status',0)
                ->page(1,10)
                ->order('id desc')
                ->select()
                ->toArray();
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        return $arr;
    }

    public function notConfirmOrder()
    {
        //处理没有确认的订单
        try {
            $arr = $this->where('order_status', 0)
                ->page(1, 10)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new \Exception("处理失败");
        }
        if (!empty($arr)) {
            $ids = array_column($arr, 'id');
            (new MallOrderGoods())->getOrderGoodsByOrderIdsAll($ids);
        }
        return true;

    }
    public function coupon(){
        return $this->hasOne(CouponCoupon::class, 'id', 'coupon_id');
    }

    /**
     * @param array $where
     * @param string $field
     * @return MallOrder|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOrderFind(array $where = [],string $field='*'){
        return $this->field($field)
            ->where($where)
            ->find();
    }
    public function shop(){
        return $this->hasOne(MallShop::class,'id','shop_id');
    }
}